package com.corpcredit.webapp.action;

import java.io.Serializable;

import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Jzds;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class JzdsForm extends BasePage implements Serializable {
    private GenericManager<Jzds, Long> jzdsManager;
    private Jzds jzds = new Jzds();
    private Long id;
    
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setJzdsManager(GenericManager<Jzds, Long> jzdsManager) {
        this.jzdsManager = jzdsManager;
    }

    public Jzds getJzds() {
        return jzds;
    }

    public void setJzds(Jzds jzds) {
        this.jzds = jzds;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        jzdsManager.remove(jzds.getId());
        addMessage("jzds.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            jzds = jzdsManager.get(id);
        } else {
            jzds = new Jzds();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (jzds.getId() == null || jzds.getId() == 0);
        // set the related jbqk as a foreign key.
        jzds.setJbqk( jbqk );
        jzdsManager.save(jzds);

        String key = (isNew) ? "jzds.added" : "jzds.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }
} 